<!--
功能：数据列表解析页面
作者：李成
日期：2023年11月30
-->
<script setup lang="ts">
import { ref, onMounted, reactive, toRaw } from "vue";
import type { pageModel } from "@/views/dataList/utils/types.ts";
import { saveDataFormd, getTableListMults } from "@/api/dataList";
import { message } from "@/utils/message";
import addICO from "@iconify-icons/ep/circle-check";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { type PaginationProps } from "@pureadmin/table";
import { getListDataMults } from "@/api/dataList";
import router from "@/router";
const generateForm = ref();
const oneModel = ref();
const isSuccess = ref(false);
const isAdd = ref([]);
const appID = ref();
const editCodeall = ref([]);
const headers = ref([]);

const datalistall = ref();
const props = withDefaults(defineProps<pageModel>(), {
  modeID: 0,
  rsID: 0,
  isSearch: false,
  tableName: "",
  isSuccess: false,
  isSheng: false,
  editCode: 0,
  paretCode: ""
});
const pagination = reactive<PaginationProps>({
  total: 0,
  pageSize: 10,
  currentPage: 1,
  background: true
});
//保存的参数
const form = reactive({
  // 左侧部门树的id
  deptId: 0,
  appName: "",
  isShow: "",
  onePage: pagination,
  sortName: "id",
  sort: false,
  filterArr: [],
  searchKey: "",
  editTable: ""
});
onMounted(async () => {
  appID.value = router.currentRoute.value.name;
  const ResultModeInfo = await getTableListMults({
    appID: appID.value,
    clientID: 1,
    subID: 0
  });

  if (ResultModeInfo.isSuccess) {
    oneModel.value = ResultModeInfo.result;
    const showTables = ResultModeInfo.result.map(
      item => item.oneModel.showTable
    );
    const appID = ResultModeInfo.result.map(item => item.oneModel.appID);
    isAdd.value.push(ResultModeInfo.result.map(item => item.isAdd));
    editCodeall.value.push(ResultModeInfo.result.map(item => item.editCode));
    headers.value.push(ResultModeInfo.result.map(item => item.tableHeads));
    form.deptId = appID[0];
    form.appName = showTables[0];
    const Resultss = await getListDataMults(toRaw(form));
    if (Resultss.isSuccess) {
      datalistall.value = JSON.parse(Resultss.data).map(item => item.dtList);
      //console.log("datalistall" + JSON.stringify(datalistall.value));
    }
    //console.log("headers" + JSON.stringify(headers.value));
  }
});
const saveJson = async () => {
  const myfalg = true;
  generateForm.value.validate().then(async () => {
    generateForm.value.getData().then(async data => {
      const Result = await saveDataFormd({
        rsID: saveData.rsID,
        jsonStr: JSON.stringify(data),
        tableName: form.editTable,
        isSheng: props.isSheng,
        editCode: editCode.value
      });

      if (Result.isSuccess) {
        message("保存成功！", { type: "success" });
        //emit("delModel", true);
        isSuccess.value = true;
        //return false;
      } else {
        message(Result.msg, { type: "error" });
        isSuccess.value = false;
      }
    });
  });
  return myfalg;
};

defineExpose({ saveJson, isSuccess, headers, datalistall, editCodeall, isAdd });
</script>

<template>
  <div>
    <fm-generate-form :data="datalistall" ref="generateForm" />
    <el-button
      type="primary"
      :icon="useRenderIcon(addICO)"
      style="margin-top: 12px; margin-left: 10px"
      @click="saveJson"
      v-if="(editCode = 100)"
      >保存</el-button
    >
    <el-button class="reset-margin" link type="primary" v-if="(editCode = 0)">
      详情
    </el-button>
  </div>
</template>
